package main

import (
	"fmt"
	"zinx/zinterface"
	"zinx/zserver"
)

// Ping Router
type PingRouter struct {
	zserver.BaseRouter
}

func (pr *PingRouter) PreHandle(request zinterface.IRequest) {
	fmt.Println("Start Ping PreHandle")
	fmt.Println("handle msgid: ", request.GetMsgId(), " handle data: ", string(request.GetData()))
	if err := request.GetConnection().SendMsg(uint32(1), []byte("Pre Ping")); err != nil {
		fmt.Println("!!!ERROR When Pre Ping: ", err)
		return
	}
	fmt.Println("End Ping PreHandle")
}

// Hello Router
type HelloRouter struct {
	zserver.BaseRouter
}

func (pr *HelloRouter) PreHandle(request zinterface.IRequest) {
	fmt.Println("Start Ping PreHandle")
	fmt.Println("handle msgid: ", request.GetMsgId(), " handle data: ", string(request.GetData()))
	if err := request.GetConnection().SendMsg(uint32(201), []byte("Hello!")); err != nil {
		fmt.Println("!!!ERROR When Pre Ping: ", err)
		return
	}
	fmt.Println("End Ping PreHandle")
}

func main() {
	zs := zserver.New("zserver")

	zs.AddRouter(1, &PingRouter{})
	zs.AddRouter(2, &HelloRouter{})

	zs.Serve()
}
